package com.example.contentprovider;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class ImageAdapter extends BaseAdapter {

    private Context context;
    private List<CustomImage> imageList;
    private int layoutItemId;

    public ImageAdapter(Context context, int layoutItemId, List<CustomImage> imageList) {
        this.context = context;
        this.layoutItemId = layoutItemId;
        this.imageList = imageList;

    }

    @Override
    public int getCount() {
        return imageList.size();
    }

    @Override
    public Object getItem(int position) {
        return imageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        if (view==null){
            view = LayoutInflater.from(context).inflate(layoutItemId,null);
        }
        //获取布局控件对象
        ImageView imageView = view.findViewById(R.id.iv_img);
        TextView textView = view.findViewById(R.id.tv_size);

        //获取数据源中第i个元素
        CustomImage image = imageList.get(i);

        //把元素数据显示到对应控件中
        //将图片转出bitmap
        Bitmap bitmap = BitmapFactory.decodeFile(image.getFile());
        imageView.setImageBitmap(bitmap);
        textView.setText(image.getSize()+"");

        return view;
    }
}
